#include "application.hh"
#include <stdexcept>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <string>
#include <stdlib.h>     
#include <time.h>       

Application::Application()
{
	this->permittedX = head.permittedX;
	this->permittedY = head.permittedY;

	auto res = SDL_Init(SDL_INIT_EVERYTHING);
	if(res != 0)
		throw std::runtime_error(SDL_GetError());
	
	if(!IMG_Init(IMG_INIT_PNG))
		throw std::runtime_error(SDL_GetError());

	window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 448, SDL_WINDOW_SHOWN);
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);


	auto surface = IMG_Load("assets/sprites.png");
	if(!surface)
		throw std::runtime_error(SDL_GetError());
	texture = SDL_CreateTextureFromSurface(renderer, surface);




	srand (time(NULL));


	int appleX = 0;
	int appleY = 0;


	bool flag;
	do{
		flag = true;
	appleX = rand()%10;
	appleY = rand()%7;
	for(int i=0; i<70;i++)
		if(appleX*64 == *(permittedX+i) && appleY*64 == *(permittedY+i))
		{
			flag = false;
		}
	} while(!flag);

	apple = new Apple{appleX*64,appleY*64};

}

void Application::handleEvents()
{
	while(SDL_PollEvent(&event) != 0)
	{
		switch(event.type)
		{
			case SDL_QUIT:
				quit = true;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
					case SDLK_s:  if(head.getPrevDirection()!=Up)head.setDirection(Down); break;
					case SDLK_w:  if(head.getPrevDirection()!=Down)head.setDirection(Up); break;
					case SDLK_d:  if(head.getPrevDirection()!=Left) head.setDirection(Right); break;
					case SDLK_a:  if(head.getPrevDirection()!=Right) head.setDirection(Left); break;
					case SDLK_f:  head.score(++snakeSize); break;
				}
				break;
						
		}
	}
}


void Application::update()
{

	head.update();
	if((head.getPosition()->x == apple->getX())&&(head.getPosition()->y == apple->getY()))
	{
	head.score(++snakeSize);


	int appleX = 0;
	int appleY = 0;


	bool flag;
	do{
		flag = true;
	appleX = rand()%10;
	appleY = rand()%7;
	for(int i=0; i<70;i++)
		if(appleX*64 == *(permittedX+i) && appleY*64 == *(permittedY+i))
		{
			flag = false;
		}
	} while(!flag);


	apple->setXY(appleX*64, appleY*64);

	}


	if(head.getPosition()->x>=640 || head.getPosition()->y>=448 || head.getPosition()->x<0 || head.getPosition()->y<0)
		quit = true;

	for(int i=0;i<69;i++)
		if(head.getPosition()->x == *(permittedX+i+1) && head.getPosition()->y == *(permittedY+i+1))
			quit = true;


}

void Application::render()
{
	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
	SDL_RenderClear(renderer);
	head.draw(renderer, texture, &sprites);
	apple->draw(renderer, texture, &sprites);
	SDL_RenderPresent(renderer);
}


void Application::close()
{
	std::cout<<"Score : "<<snakeSize<<std::endl;
	SDL_Quit();
}

int Application::run()
{
	auto prevTicks = SDL_GetTicks();
	render();
	while(!quit)
	{
		if(SDL_GetTicks()-prevTicks >= 250)
		{

			handleEvents();
			update();
			render();
			prevTicks = SDL_GetTicks();
		}
	}
	close();
	return 0;
}
